package com.pine.dao.pineframework.mapper;

import com.pine.common.database.BaseMapper;
import com.pine.dao.pineframework.entity.SysDept;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * <p>
 * 部门表 Mapper 接口
 * </p>
 *
 * @author pine manage
 * @since 2024-08-12
 */
@SuppressWarnings("all")
public interface SysDeptMapper extends BaseMapper<SysDept> {

    String BASE_COLUMNS = "id, `name`, `code`, parent_id, sort, `status`, create_time, create_user_id, update_time, update_user_id, deleted";
    String D_BASE_COLUMNS = "d.id, d.`name`, d.`code`, d.parent_id, d.sort, d.`status`, d.create_time, d.create_user_id, d.update_time, d.update_user_id, d.deleted";


    /**
     * 递归查询部门和子部门
     */
    @Select("WITH RECURSIVE sub_depts AS (" +
            "SELECT " + BASE_COLUMNS + " FROM sys_dept WHERE id = #{deptId} and deleted = 0 " +
            "UNION ALL " +
            "SELECT " + D_BASE_COLUMNS + " FROM sys_dept d " +
            "INNER JOIN sub_depts sd ON sd.id = d.parent_id" +
            ") SELECT * FROM sub_depts;")
    List<SysDept> listDeptAndChild(@Param("deptId") Long deptId);
}